trigger Account_DeleteUndeleteLeads on Account (after delete, after undelete) {
    
    Set<Id> AccountIds = new Set<Id>();
    
    if(Trigger.isDelete){
        for(Account a : Trigger.old){
                AccountIds.add(a.Id);
        }
        if(AccountIds.size()>0){
            List<Lead> DeleteLeads = [select Id from Lead where Previous_Account_ID__c in :AccountIds and Account__c = null and IsConverted = false];
            if(DeleteLeads.size()>0) delete DeleteLeads;
        }
        
        // Update leads from merged accounts so they'll get new info
        if(AccountIds.size()>0){
            List<Lead> MergedLeads = [select Id from Lead where Previous_Account_ID__c in :AccountIds and Account__c <> null and IsConverted = False];
            update MergedLeads; // Not updating anything, just getting the trigger to run
        }
    }
    
    if(Trigger.isUndelete){
        for(Account a : Trigger.new){
            AccountIds.add(a.Id);
        }
        if(AccountIds.size()>0){
            List<Lead> UnDeleteLeads = [select Id from Lead where Account__c in :AccountIds and IsConverted = false and IsDeleted = True ALL ROWS];
            if(UnDeleteLeads.size()>0){
            	try{
            		undelete UnDeleteLeads;
            	}catch(exception ex){
            		// To-Do: Add fancy error handling for Leads. For now it will not undelete Leads if there is a failure.
            	}
            }
        }
    }

}